package com.glorystone.ins.common.util;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liyusong
 * @version 创建时间：2018/1/5 16:30
 */
public class ReflectionUtils {
    // 通过反射获得所有属性的field数组
    public static Field[] getFields(Object model) {
        Field[] field = model.getClass().getDeclaredFields(); // 获取实体类的所有属性，返回Field数组
        return field;
    }

    // 通过get方法获得属性的值
    public static List<Object> getFieldValue(Object obj) {
        try {
            List<Object> list = new ArrayList<Object>();
            Class<? extends Object> clazz = obj.getClass();
            Field[] fields = obj.getClass().getDeclaredFields();//获得属性
            for (Field field : fields) {
                PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
                        clazz);
                Method getMethod = pd.getReadMethod();//获得get方法
                Object o = getMethod.invoke(obj);//执行get方法返回一个Object
                list.add(o);
            }
            return list;
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    // todo ???
    // 通过set方法给属性赋值
    public static Object setFieldValue(Object obj) {
        Class<? extends Object> clazz;
        try {
            clazz = obj.getClass();
            Field[] fields = obj.getClass().getDeclaredFields();//获得属性
            List<?> fieldValue = getFieldValue(obj);
            for (int i = 0; i < fields.length; i++) {
                PropertyDescriptor pd = new PropertyDescriptor(fields[i].getName(),
                        clazz);
                Method getMethod = pd.getWriteMethod();//获得set方法
                //todo
                getMethod.invoke(obj, fieldValue.get(i));//执行get方法返回一个Object
            }
            return clazz;
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }


    // 获得属性的类型
    public static List<String> getFieldType(Object obj) {
        try {
            List<String> list = new ArrayList<String>();
            Field[] fields = obj.getClass().getDeclaredFields();//获得属性
            for (Field field : fields) {
                String s = field.getGenericType().toString();
                list.add(s);
            }
            return list;
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String setMethod(String fieldName){
        if (null == fieldName || "".equals(fieldName)) {
            return null;
        }
        int startIndex = 0;
        if (fieldName.charAt(0) == '_')
            startIndex = 1;
        return "set"
                + fieldName.substring(startIndex, startIndex + 1).toUpperCase()
                + fieldName.substring(startIndex + 1);
    }
}
